Clean the codes a bit, mostly moving functions inline to instance defini...

...tions and simplifying