trying to speed things up - now only loop through valid combinations