Stop window opening and closing 3 times on startup

Caused by calling setMode several times. Fixed by making a wrapper
function which only calls through to setMode if the requested mode is
not already set.