Factor out completion functionality (closes #4)