Tidied up code + added comments