Implemented test cases, plus some small fixes.