Because of the ad hoc nature of downstream use of this code, things will undoubtedly break when changes are made. It should be packaged as a standard R package.