added tests, fixed a lot of badness