refactored and added some tests