Added some of Nebuleon's optimizations from master