Optimize memory operations

Hello!

Please review this code, I removed memory reallocations but save loading file into memory.