fix for bad logic when fixing #85 , and fix unittests