Cleaned up code. Should run on both Linux and OSX.