First commit
Added test cases and fixed bugs that they uncovered.