cleaned up code, runs in 0.242s