Added guards to ensure that prev and val exist before calling their keys function