Adjusted the binvert routine to more closely match Michael McCarthy's original