Fixing variable scoping issue which broke a few things