Adding assertions and exception handling (where missing).