various improvements and cleanups

Please refer to the commit messages.