Stability fixes and unit tests