Added license information and tidied up code