Refactoring.

Refactored based on Kun's comments.
Added config file and functionality.