Fix a few invalid reads, prototype out map freeing function.