Refactor scrollTo function