Minor improvements.

1. Changed a long `while` loop to a short `for` loop.
2. Altered a type to make the code more clear and to help cut back on typecasting.