add validation and minor bug fix