Refactor app structure, move lib functions to a better home