First revision of working code