Multiple fixes for out-of-bounds reads (mostly off-by-one errors), static and const changes, hidden visibility improvement, minor CMakeLists cleanup

Compile tested on Linux with gcc-4.7.3 and clang-3.4.1.

Please, merge this pull request!
Thank you in advance!