Various bug fixes (Fix #4, Fix #5, Fix #6)