Cosmetic refactoring
Improved error handling