Got rid of templates and optimized code