factor out code