Fixed the bug in computing residuals