Various bugfixes. Now it seems to be working quite well