Bugfixes and restructuring