Bugfixes, still needs more debugging