Added tests and fixed scope for variable