Refactor scrolling code