Fix some bugs like infinite loop on downloads' end