fixed a nan check bug, and a for loop bug