use loop instead of recursive call - fixes stackoverflow on windows