Adding tests.Fixing bugs