added comments and improved coding style
added template options