modularized python solution further