Added check for nsamples being a valid number