(part3 updated): remove unnecessary malloc/free