Fixing crash that could be caused if the sleep function was interrupted