final version with validation