fixed example and typos
improved code robustness