added cases for mode change, serious cleanup