key generation and invalidation fixes, more tests