Fix #301 by checking for valid keys.

Have also added a default value for the session 'valid_keys'
variable