Rewrote this solution to be much, much faster