adjusted placement of params
Fixed loops and stack overflow problem