fix bugs found by unit tests