Fix bugs, strange things and moved desired distance in z to global var