add unit tests, refactor to use Optional