Remove wrong malloc
Add more error checking to serialise code