Added test cases and documentation. Minor refactoring.