More formatting fixes
Remove duplicated code by using set_changed
Simplify the handling of hashes, remove unnecessary variables
Declare more variables inside loops
Declare all functions at the start of the line