Refactor the code, add validations