optimize function for unit test