Improve test coverage and move exception handling