Add copyright and minor refactoring