Updating stat calculation. General code cleanup