full refactoring with lots of simplifications and unused cases removed
discard changes from master