Library refactor

Minor style and flow refactoring