Due to an influx of spam, we have had to impose restrictions on new accounts. Please see this wiki page for instructions on how to get full permissions. Sorry for the inconvenience.
The code was a bit hard to follow. Use clear/explicity variable names and comment a bit more on what is going on.
Also fold the double list iteration into a single one
Something feels wrong though