Added template define and some more code cleanup