working version with validation
added 'end' code