Refactor and fix bug that broke calculations when only one element was within the viewport