rewriting the library to be more functional, and elegant