Fixed a bug
Fixed global variable bug and added tests