Added header output and minor refactoring