Merge Gasta's additions into master
Refactor code for general cleanliness