Fix issues uncovered by unit tests