Unittests

Added unittests, coverage at this moment is around the 82%.
Most none tested code are closures at this moment these could of course be ignored if needed.

Some minor refactoring has been done to enhance testibility or actually make it possible to test. For now these have been kept as small as possible and in general be extracted to their own function. In the future when needed these can be of course be extracted to a higher level.