improve error handling
merging latest changes