Rebased manual diff from 3d-tom. SUCH COMPLEX :|