Added more error handling tests and updated code accordingly