Fixed some bugs, added option to log output to file.