improve speed and fix bugs