refactor and fix bugs that resulted from it