Moved scope of variables + imports

Moved scope of variables into try catch and added a few imports for
SharedPreferences and Context