Some clean up. Changed generate templates function for a little more flexibility.
Mostly code cleanup