fixed code flow errors