factor out common finishing code