Close window properly