fix variable naming and bugs