Support the X window resize

Also fix several corner cases