modifying some stuff to avoid arithmetic exception