Some bugfixes, additional error checks