Refactor to reduce code duplicate