optimized script to use ternary logic