fix bugs + optimize