Tidied code (after re-reading it!)