improve error handling and minor fixes