Fixed numerous bugs and inconsistencies.