initial import from http://pastebin.com/w8LEtACy - now to customise it to my own requirements