Fix a few bugs and test it