Added histogram, performed a small reorganization of the code.