Added proper logging and cleaned up a bit.