I attempted to fix the pascal code, but thought that the branching calls were better represented this way.