Optimize keyframes more effectively (fixes #21)