add logic that decides how many buffer rows are required