refactor lib and views, move some functions to lib