Major refactoring of library