tested and fixed on linux, bog on Mac OS X