Fix #207, clean up code