Tidied code and added full comments