mempool memory leak fix and some code cleanup