All known leaks fixed, bugfixes, refactoring