cleaned up code + explenation