Cleaned up code, made some stylistic and performance enhancements, added options.