refactor code for lib