A bunch of optimizations fixes and improvments

This pull request contains many features from other contributors. See graph or commits for details