More fixes

A medly of improvements and code used in future pull requests. Each commit is independent of the others.