Bug fix & other general refactoring

Fixed a bug in transitions from/to the same state.