Removed lots of code duplication in mesh.