Refactor loopback alignment. add unit tests