Fix logic & replace hardcoded settings with global variables