update to only loop once instead of iterating twice over the same array.