Reworked state change mechanism
Modified code with testing facilities
Version ready for merge with master.