I think you need to check visibility in this loop and the one below