Optimized version, added command line options.