Re-factored code a little bit, split into separate files.