clean up and fixed long standing mem leak
clean up and fixed long standing mem leak