Remade some code to be more understanable