Refactor and add validations, ugly but easier to read/understand IMO