Super slow solution, but worked in 47 minutes