Re-factor code into a proper library.