Added logic for a recursive forward pass