Refactoring: moved some state-code to appstate