Bugfix, free memory at the right time.