Corrected indenting and moved code to make it easier to read