improved efficiency by removing for loops