Rewrited some parts to redder to improve stability.