added formula and fixed a bug