Cleanup and some bugfixing