refactor and fix small bugs