Added tests and some minor cleanup