Slight code speedups

This PR makes the code a bit faster, although it also makes the code a bit uglier.

Basically, there are two types of changes:

* Disable some bounds-checking using the `@inbounds` macro
* Pull indexing operations out of loops when possible