added test cases and some refactors