Better error handling implemented