correct variable changes and placement  of certain blocks of code