Added more error checking with try catch blocks