Importing some more changes from Junjie.

Note that the filtering does not work yet.