key calculation function completed (not tested)