fix bugs and speed issues