Factor out refresh_window