refactored solution 2 to not call findSide every time but keep the previous one at each iteration.