Bugfixes and Refactoring