Eliminating redundant loop in both visa function