More bugfixes based on tests