A little refactoring and cleanup