Refactoring and improving security