Make it remove windows properly

Previously, it wouldn't remove closed windows from list, thus
causing invalid dereferences.