Original code is opaque as hell. Treat this function as horribly flimsy until tested.