Fixed headers sent issue, some other changes