Clean the CRS function and add unit tests