optimize loops, algorithms, etc.

Also update coding style (it's my branch, now...)
Merge pull request #1 from impinball/experimental

optimize loops, algorithms, etc.