Added tests and fixed associated bugs found