extract clear_screen function.
move freeing memory to the function that allocated the memory.