comments added and started follows algorithm