started refactoring, broken commit for fix by bgraf