improved error handling with exceptions