Reorganized code to clean up some boilerplate.