Fixed a few memory "out of bounds" bugs, added unit tests for those and
improved some ("old") unit tests.