added tests, fixed many bugs