Some refactoring/renaming
Improved error handling