Updated version of Taidgh's code