Various security improvements and bug fixes