Checked through memory allocations and valgrind agrees