Incorporated try & catch blocks as well as making it cleaner