put everything inside decent functions

I also added a main script for a final integration of all functionality
in the near future.