Added useful utility function and finished resid