First version, code needs some cleaning