Issue #5636: Fix many bugs