Cleanup and bugfixes