Better exception management. Code refactor