Various fixes, cleanups, some refactoring ;)