Reformat code and add unit tests