Sorry for the big diff, i'm too lazy to chop it into smaller pieces.

This hasn't been tested thoroughly yet, since proper testing takes a long time.

Move all the line parsing/mapping stuff into classes.

Move the line iteration into a single function.

Be more verbose.