refactor options handling.
add tests.