here is comments and code rearranged slightly