Updated code clarity and tested on mac.