#312 fixes with unit tests