Working version. Needs style revision!