Mostly working. Have some edge cases to figure out.