Converted two loops into one for small performance improvement.