optimized code to use less cpu