modified array version