simplification and fixed issue with error handling