Factored out a lot of handling from main() into separate subs
Explicitly return from all functions
Fixed some usless interpolation and missing error handling
Changed stringy split() to regex split() for clarity