Refactor mail checking out to lib