Added tests and fix small bugs