rpi: applied Stefan Weil's patch (code cleanup and improvements).
rpi: code cleanup