move more logic from test file to lib file to be able to use the lib in other places