LOTS of refactoring

Makes the code a lot more readible