Removed unused functions and generally tidied up