Improve error handling and add multi-hostname support