Exception handling refactor. Close #188