Fixed p_valid sampling

This PR is the same as #249 except that the target has been changed to release-v0.5.4.  See PR #249 for previous discussion.