fixed potential bugs and added more tests