Slightly big PR because I started refactoring to reduce code duplication between the UI based business logic and the api one.