Cleaned & tidied up the whole code