Code cleanup and crash fix during bg downloading