Hopefully finished slave code. Master code still skeletal. Attempted to clearly define common functionality