fix various bugs and correct some security issues