fixed multiple serious bugs
removed dead code