Merged Shi's and Jared's code