fixed bug with template directory
polished and commented code
Fixes and comments