cleaned the code and added secure option