Fixed allocation memory bugs