Rewrite function Cifi and make it more efficient