correct bugs and add validation function