Fixed the sequence problem. Moved all markup to template. All logic went to the actual php file.