Addressed to the best of my abilities, with several TODOs left.  Fixing this more thoroughly requires digging deep into the core, so @sigurdteigen and other experts should pursue it.