Fixed bugs on logic