Fixed header bugs and revised code