Fixed two bugs in sample and residuals