Better algorithm (lazy Google-fu)