Refactor some common code