Code tidied and fixed