few optimizations
fixed key management