Refactor #3935: Fix some bugs.

Refactor #3935: Fix some bugs.