Fix some minor bug and start adding exception handling