refactor, added some tests