Rewritten, to be a self contained class