fixed some bugs, added main for tests