MASSIVE cleanup and bugfixing