First version of complete code