Fix a few bugs and add a simple test