Bugfixes, refactoring, speed improvements