Move working directory setting code to a function

Also add a docstring to the start function.