refactor and various bug fixes