Here is the code - https://drive.google.com/file/d/0B5Srrp8d3ZYCaG9ITVk4SEdMWEE/view?usp=sharing - thanks to @viktor_kuzev