add error checking for mallocs
fix error in map indexing