Did some refractoring of things.

Need to debug the system though.