Make this piece of code into a library.