Initial refactoring attempt

Give me your thoughts!