add some tests and refactor processing