factor out completion logic