Replace for loop with join