Seperated logic from ui.
Applied CGL to ai module.