speed and memory use improvements