Refactored code as per api document + added some validations