improved error handling
fix a bug