Added Exception and improved Code