Fixed some bugs and started to implement tests.