Tidied code and commented