refactor switch states to functions