improved error handling

+ some bug fixes