improved tpi script