fixed bug with error handling