Some bugfixes and tests.

This is the first runnable version but with no function.