refactored to split functions between host and slave.