The code was broken

push puts the element in the end of the array, so the next line only works if firstelement = last element, aka the array was empty. If it wasn't it won't work.