First tested version 
Debug code included