added validation and fixed many bugs