The Error about not being able to modify header information is 9 out of 10 times caused by white space above your Doctype Declaration and the HTML.
Many times, an inadvertent space added after a closing a PHP tag is the cause.
While it is ok to start new PHP blocks on a line down - you need to ensure there is zero chance of an actual space behind each closing PHP block.
You could even go as far as closing one and starting the very next one right after it like this: " ?><?php "
I would go through it - clean up any spaces and then try it again.