Made a few changes to make the code smaller and less readable. Runs in about 1.337s on my laptop.