Moves endless scroll code into a function