Performance enhancements

Some performance enhancements I found absolutely necessary. See the comments in the individual commits.