Your logic here is a bit tangled.

For instance, to find the maximum value, you need to do something like this:

max = 0

repeat for each list element:

x = next list element
if x > max then...