Added error handling and return the error obj