fixed undefined ref error and handled unlink error