replaced queue with extendable array; now read whole file into memory
and handle loops more efficiently