Refactored quite a bit
Further refactoring, added command-line interface