Hotfixes cherry pick

These are several fixes included in #599 which are better included now since the more I tweak the code to fix #546, the more I think I need to redo #599.