some fixes to the logic