reworked the median function and added some fork code