cleanup; fix free memory function