added more error handling and possible error fixes