implemented key() algorithm