Cleanup: Move the utility code to its own file. Add header comments.