optimize pkey code
more unit tests