Rewrite function to be able to write unittest