Heavy cleanup, bugfixing etc.