added tests and fixed many issues