initial commit with code from http://pastebin.com/DTTW3R2r