finished code for iterative update and predict