Cleanup in _compatibility

Mostly just refactoring, with some minor improvements. Also removed a duplicate `reduce` import. I hope the result is a bit easier to follow :) (These are the changes I made myself when trying to debug the absolute_import problem).