Make a beginning of a library.

I refactored the code to be able to be used as a module.