Refactoring and fixes

Cleaned up a lot of code, removed unnecessary dependencies between classes and made minor bug fixes.