Refactor code for testing and add a simple test