Take care of NaN or Inf in residuals or endogenous values

Also adds more debugging information. Closes #491