Rebased to use a for loop instead.