Encapsulation of code

Some routines have been put into a class, much cleaner now