Initial refactoring
Fixes after refactoring