Code clean up and a little refactoring