Crude and untested version of the code