Refactored buffer shrinking into method. Fixed shrinking bug when index is 0 by checking for null.