Some cleanup and bugfixing