Lots of improvements

This pull request bundles up many bug-fixes and enhancements from myself and two other people.