Added more robust handling of keyframes.