Added more template stuff.
Made code more organized.
Separation functionality into functions.