Removed some code duplication from mesh.h by using templates.