moves some code to the top and fixes some indentation