Cleaned up to be turned into a library