Working version which needs some fixes.