tidied up source code