code refactored and added validations