moved given code to separate files, and cleaned it up a bit