improve error handling
fix a bug