optimizations for memory and speed