Improved window handling