Added cleanup routine to close resources