corrcected order of code flow