Overhaul of the code to fix inconsistencies with variable declarations