Refactor code, move settings to python files and add error handlers.

Also, change the console output's implementation to collections.deque.