Backout of #3513 (except formatting)

Fixes #3545