add validation and fix a bug