Refactor common function into seperate file