Refactored security stuff