Removed some side effects from functions and genericised some behaviour.