now works with any value of n