Clean up patch and add unit tests