- Identified bottleneck with a comment. Probably the cause of the memory exhaustion
errors

- changed all instances of $id++ to ++$id. Not an important optimization
as this is already done by the php compiler but its nonetheless better practice.

- commented out an unused initialization preceding a foreach loop.