rewrite the completion algorithm