Tweaked for generality.