added subroutines and fixed template