Merged Abhi's code. Finished implementation.