Added license information and tidied up the code