Added memory freeing code