refactored, added some tests