Refactor the lib code