Fixes lots of bugs, optimizes algorithm, and completes program