completely works, but super slow for n>2