Corrected _MANY_ bugs thanks to valgrind