fixed a major bug and added more tests