Tidied and commented code