Kfsti-668

This PR replaces #1307. It includes all changes there, a few other fixes, and a giant squash of 200+ commits.