Refactoring performed (extract method) and bug fixed (IndexOutOfBoundsException).