[Closes #13] Re-factored logic and fixed several bugs