changed code flow slightly