fixed bug and added validation