First draft finished of refactoring