Re factored code and added some validation code.