Lots of room for optimization but it does the job in ~2 mins.