fix bugs and handle exceptions