fixing bugs in logic. Still non-stable