Cleanup of loop code for scanning. scanCompleted callback is sent dynamically
Fixed merge conflict