Add some optimizations from Hundsbuah