Added error handling and basic logging