fixed window handling. I think I've got it this time