Added logic to javascript to add rows appropriately