fixed some leaks, invalid reads/writes