Umm.. refractoring, error fixing. Too nasty to commit one by one.