Refactor rsync and add tests