added free for malloc to prevent mem leak