Add license header
Fix some memory related problems