added several cases of proper error handling