Initial attempt. Solves, but pretty slow.