refactor. fix minor bugs