iterative implementation

IE8 doesn't handle deep recursions well, so here is iterative implementation.