refactor a little bit, make it clear and more testable