Made the code more object oriented and added input validation.