Refactoring in progress
Refactoring finished, now need to fix tests and bugs