Cleaned up code, fixed some minor bugs, added logging and one more config option.