Refactoring code in themap.cpp