ported from x11 branch