move ncurses cleanup inside of state check