Improve safety and fix bugs