Abstracted some values and added error handling