Test coverage improvement

Added a whole bunch of tests, fixed some docs and edge cases.
It looks like we panic with "impossible" if p == NaN. Could you fix that? I think the correct way is to change the first comparison to 
if !(p >= 0 && p <= 1){panic("out..")}