Refactor loop stuff.
Replace magic values with symbolic constants.