various bugfixes, added some error handlng