comments added and code simplified