replaced assertions with throw exception, improved error handling