more optimizations. Not exactly tested.