Moved some logic out of integration