added error handling on several cases