refactor the logic of adjusting sliding window.