Modifed Code and Added Exception Handling