modified global state var, added handlers for ending