Fixed missing end and close functions causing memory leak.