fixed many bugs. need for more tests