refactoring and secur bug fix