Refactoring key utils and tests