edited code to fix bugs