Authentication and a little refactoring