functions tidied up