fix bug in termination condition and wrapping function