Fixed and tidied up the map code