Added discretization and fixed some bugs.