add exceptions, fix bugs and optimize code