updating with new code for 1-d array (bug removed)