Bugfixes; simplify some code.