optimized script for MacOS/Linux