Fixed lots of bugs.
Brought repeated chunks of code out into functions.
Print logs to console.