fix bugs and faulty logic