Fix refactor bugs