Changed transition algorithm

Also fixed some bugs