Changed handling loop to use uid functions to make sure that we process the
correct mail even if changes happen during processing.