Fixed algorithm saving all states and making sort after each step. Did some refactoring.