New PR for #880

This is the same PR as #880, with some commit wiped out, for easy rebase in the future.