added free memory calls, other fixes