More effecient key encoding, added code documentation