Refactoring And various small fixes suggested by Ayush.