used python functions for efficiency - it takes a while to run on larger files esp on Pi