Merged Anants code and fixed some issues