refactory code for infinity scroll