performance improvement

stack.add recreates whole stack in every iteration and it takes substantial amount of time.
For ~500 inputs it took around 2s, this improvement cuts it down to ~500ms