add unit tests and refactor functions to handle edge cases