Improved text parsing code for easier reading and modification