Refactor algorithm and view

for ease of extension.