Only define dtop once

`dtop` was unnecessarily defined twice.

I have also moved the variable definitions up on the top of the `function` since these don't rely on anything else happening before they are assigned.