updated mode calculation, improved a bit of readability