Optimize the shit out of map reading, fix map writing, add regression tests.