abstract some duplicate code