Updated and tidied up code.