use length/2 for a shorter and less error-prone solution