Almost there. Needs test cases.