use median for robustness
fix for median compile error and divide by 2