code polish and free memory after allocation