re-arrange p-value func + minor fixes