added validation, and refactored code for tighter MVC implementation