refactor and document key.py
refactor and expand tests