Minor tidy up. Reformatted the code slightly and moved two variables to reduce their scope.