Added code and docs from #5269 (some code heavily rewritten) and removed old workarounds; fixes #5269