Minor code arrangements
Add some tests