added tests and cleaned up some of the non-reusable template logic.