Optimizations and Upgrades

This pull request add a lot of fixes, upgrades and optimizations. See the commits.