Code cleanup and template simplification