Moved forward propagation code to a function