Separate into static lib and refactor