Fix several memory issues.

These commits fix remaining memory issues from #221.