Added memory free logic.