Fixed numerous bugs