The code would be easier to understand if it were properly formatted. The statements should be indented for nested logic (ie inside loops) and there should be {}s around the code in each loop.
...