added some optimizations #1162