Optimize conditionals, loops, etc.

Optimized several conditionals and loops with refactoring. I'm up for any questions (please give line numbers if you spot any issues). There should be few other things changed.

I tried to comment most of my changes.

Also, please pay attention to the comment on line 1778 (relative to my patch): I think I *might* have found a bug, but I'm not entirely sure. I would definitely enjoy some clarification if that is intentional. the reason is because I went to unroll part of the loop to get rid of extra conditionals that would be known right off the bat, but the more optimal equivalent is quite different for each one. I have the suggested fix commented out, but they both are easy to comment out/uncomment.
The original code segments regarding my uncertainty are the `i > 0` in the ternary at line 1745 and the `if (i >= 0)` at line 1748.

I'm considering forking this myself and storing a lot of the true/false information in a mask to further enhance speed. I would also make plenty of code style changes, but it would be my own project.