Slight code speedups

Assert indexing operations are in bounds
Pull indexing operations out of loops when possible
Merge pull request #3 from johnmyleswhite/speedup

Slight code speedups