added safeguard for malloc, added add_tail and add_head