Solution 28 refactoring

Replacing while loops with for loops