Bunch of fixes observed during hot standby testing, fixing memory leaks, and tidying API