#3 refactoring; slight security changes