Added sophisticated map management, consolidated some code