added consts to function arguments where variable doesn't change internally. made some other variables volatile.