Major refactoring, still WIP. Rebase will be done at later time