added the preprocessing code (clunkily), and some more cleaning of the to-be main() function