Added length variable to keep track of the number of elements in the main array