Logic rearranged for scroll listener