WORKING CODE (almost)