Fix various bugs (#2 #3)