Improved code

Merged two loops into one without adding complexity.