Fixed major logic error in transitions and state time and refactored code