Optimizations, bug fixes, special conditions

The commit messages discuss the details.