Some bugfixes + cleanup