cleanup and a little refactoring