Improve handling of err
Update 'getSecretKey' function