Fix several bugs in constraints.
Bigin refactoring