working version tested on linux/OS X