Loop-invariant optimization.
Removed some function calls to minimize function call overhead.
This makes for some less pretty code but since PHP doesn't
support inline expansion not much can be done about it.