Fixed bugs and adapted for OSX 10.8