much better impl of #406 + completion-related refactorings