Fixed some memory and closing issues.