Improved code and added abstraction to handle authorization errors

Closes #11