Bugfixes, simplified code