Added some optimizations recommended by scottgonzalez