added consts and improved readability of pointer checks in build_map