done some code refactoring and added some tests