code clean up & prevent memory leak (more codes will be checked in next
commit)