Added lots of tests and some adjustments to invariant checks