refactor validation function