improve error handling with exceptions