Code cleanup and added state transition.