Rework boundary conditions and add error reporting, test cases.