iterative instead of recursive