Fix several php errors/notices and added checking for template directory write permission