Added map changing and fixed a memory leak