fix batch mode, and fix optimized closest position routine