Improve lib by moving separated functions to lib