added error handling
added error handling and shuffled around a bit