Do not poll events when application is closing
Removing unnecessary nested loop structure and unused variables from paletteMap.