Cleaner attempt to fix #827

This is a less ugly fix for #827.
The implementation is temporary, since a major overhaul is upcoming, but the virtue in these commits lies in the unittests