clear flags, clean up code