Refactoring and better errors