dynamically malloc/free arrays to avoid stack overflow on windows.
starting with 0 for array index will give a reasonable value for 'first'.