Bugs fixed. This is the first version which can pass some tests.