Added residual functions to make sure I didn't make mistakes.