Small bug fixes and some logic changes