Make code more modula and add test cases.