Refactored slightly, runs in 8.9s