Final cleanup and bugfixes (probably)