refactored with states and comments