more memory optimizations and tests