This very clever code should be untangled