~30% speed increase by replacing brute-force approach with slightly less brute-force approach