Almost rewrited full code