merged Tian and Neel's code