Added error handling for plain js