Some tests added, quite a lot of bad bugs squashed

There are written tests for two functions. These are two of the highest
level functions, which depend on most of all other code to work.
So although there may seem as almost no test coverage, it is actually
quite good.
For the moment this will stand as it is, since the project must progress.