Fix #3755

Significantly simpler and shorter than what we had previously. Should
be more maintainable.