Final commit.

Optimized some code. Replaced some horrible hardcoding with a for loop.
It makes a lot more sense now.