Refactor user functions to lib package

Opening a new PR for this, this commit will be axed from my other PR.