Refactored the autoscrolling logic.