rewites and simplifies library