Refactored and enhanced the security system