Fixed issue #2 and added state transitioning