Refactored a bit for that OO flavor. Not well tested