Exceptions refactoring

Exception refactoring. Bug fixes.