Add comments and licences and correct minor bugs