did some exception handling and performance optimization