Refactored change dir functionality

Also ensured memory is properly being freed.  All good so far. :)