Replaced basename and dirname with fancy variable handling.