Did some refactoring for better exception throwing