House keeping with added comments and have added some minor error handling