Added testing framework and some refactoring